package com.bff.gaia.mix.api.xjoin2.client.rdb.async;

import com.bff.gaia.mix.api.xjoin2.client.rdb.table.RdbSideTableInfo;
import com.bff.gaia.mix.api.xjoin2.core.side.SideTableInfo;
import com.bff.gaia.mix.api.xjoin2.core.table.FieldInfo;
import com.bff.gaia.mix.api.xjoin2.core.table.JoinInfo;
import com.bff.gaia.mix.api.xjoin2.core.table.StreamTableInfo;
import com.bff.gaia.mix.api.xjoin2.core.table.XjoinInfo;

import java.util.List;

public class RdbAsyncXjoinInfo extends XjoinInfo {
	public RdbAsyncXjoinInfo(StreamTableInfo streamTableInfo, SideTableInfo sideTableInfo, JoinInfo joinInfo, List<FieldInfo> outFieldInfoList) {
		super(streamTableInfo, sideTableInfo, joinInfo, outFieldInfoList);
	}

	@Override
	public void buildQuery(JoinInfo joinInfo, SideTableInfo sideTableInfo) {
		RdbSideTableInfo rdbSideTableInfo = (RdbSideTableInfo) sideTableInfo;

		sqlQuery = "select ${selectField} from ${tableName} ";
//		String sqlCondition = "where ";
		StringBuilder sqlCondition =new StringBuilder();
		sqlCondition.append("where ");
		for (int i : sideTableKeyFieldPos) {
			String equalField = sideTableInfo.getFieldList().get(i);

//			sqlCondition = sqlCondition.concat(equalField).concat("='?' ").concat("and ");
			sqlCondition.append(equalField).append("= ? ").append("and ");

		}
//		sqlCondition = sqlCondition.substring(0, sqlCondition.length() - 4);
		sqlCondition.delete(sqlCondition.length() - 4,sqlCondition.length());
		sqlQuery = sqlQuery
			.replace("${tableName}", rdbSideTableInfo.getTableName())
			.replace("${selectField}", sideSelectFields)
			.concat(sqlCondition.toString());
		System.out.println("---------V4 side_exe_sql-----\n" + sqlQuery);
	}
}